package com.dm.common;



import com.dm.config.KafkaConfig;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.PrintWriter;
import java.io.StringWriter;

@Component
public class KafKaUtil {

    @Resource
    //保存着Kafka配置信息
    private KafkaTemplate kafkaTemplate;

    @Resource
    private KafkaConfig kafkaConfig;


    /**
     * 发送普通日志方法
     * @param message
     * @return
     */
    public String sendInfoMessage(String message){

        try {
            kafkaTemplate.send(kafkaConfig.getModuleName()+KafkaConfig.LogType.INFO,KafkaConfig.qgKey,message);
            return "success！";
        }catch (Exception c){
            System.err.println(c.getMessage());
            return "error！";
        }
    }

    /**
     * 发送异常日志方法
     * @param e
     * @return
     */
    public String sendErrorMessage(Exception e){

        try {
            StringWriter writer=new StringWriter();
            PrintWriter printWriter=new PrintWriter(writer);
            e.printStackTrace(printWriter);
            kafkaTemplate.send(kafkaConfig.getModuleName()+KafkaConfig.LogType.ERROR,KafkaConfig.qgKey,writer.toString());
            return "success！";
        }catch (Exception c){
            System.err.println(c.getMessage());
            return "error！";
        }
    }
}
